package com.csoa.community.db;

import android.database.Cursor;

import com.csoa.community.bean.IssueGroupBean;

public class IssueGroupDB extends DataBaseDB {

	/**
	 * 课题组插入
	 * 
	 * @param lm
	 */
	public static void insert(IssueGroupBean issueGroupBean) {
		DataBaseDB.getInst().getDB();
		String sql = "insert into IssueGroupTable(unit,principal,writer,area,valley,classification) values(?,?,?,?,?,?)";
		db.beginTransaction();
		try {
			db.execSQL(sql, new String[] {
					issueGroupBean.unit,
					issueGroupBean.principal,
					issueGroupBean.writer,
					issueGroupBean.area, 
					issueGroupBean.valley, 
					issueGroupBean.classification});
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
	}

	/**
	 * 查询某个课题组信息
	 * @param lm
	 */
	public static IssueGroupBean query(String id) {
		DataBaseDB.getInst().getDB();
		String sql = "select id,unit,principal,writer,area,valley,classification from IssueGroupTable where id=?";
		db.beginTransaction();
		Cursor cursor = null;
		IssueGroupBean issueGroupBean = null;
		try {
			cursor = db.rawQuery(sql, new String[] { id });
			if (cursor.getCount() > 0) {
				cursor.moveToNext();
				issueGroupBean = new IssueGroupBean();
				issueGroupBean.id = cursor.getInt(cursor.getColumnIndex("id"));
				issueGroupBean.unit = cursor.getString(cursor.getColumnIndex("unit"));
				issueGroupBean.principal = cursor.getString(cursor.getColumnIndex("principal"));
				issueGroupBean.writer = cursor.getString(cursor.getColumnIndex("writer"));
				issueGroupBean.area = cursor.getString(cursor.getColumnIndex("area"));
				issueGroupBean.valley = cursor.getString(cursor.getColumnIndex("valley"));
				issueGroupBean.classification = cursor.getString(cursor.getColumnIndex("classification"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;

			}
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
		return issueGroupBean;
	}
	
	
	/**
	 * 查询某个课题组信息
	 * @param lm
	 */
	public static IssueGroupBean query() {
		DataBaseDB.getInst().getDB();
		String sql = "select id,unit,principal,writer,area,valley,classification from IssueGroupTable";
		db.beginTransaction();
		Cursor cursor = null;
		IssueGroupBean issueGroupBean = null;
		try {
			cursor = db.rawQuery(sql, null);
			if (cursor.getCount() > 0) {
				cursor.moveToNext();
				issueGroupBean = new IssueGroupBean();
				issueGroupBean.id = cursor.getInt(cursor.getColumnIndex("id"));
				issueGroupBean.unit = cursor.getString(cursor.getColumnIndex("unit"));
				issueGroupBean.principal = cursor.getString(cursor.getColumnIndex("principal"));
				issueGroupBean.writer = cursor.getString(cursor.getColumnIndex("writer"));
				issueGroupBean.area = cursor.getString(cursor.getColumnIndex("area"));
				issueGroupBean.valley = cursor.getString(cursor.getColumnIndex("valley"));
				issueGroupBean.classification = cursor.getString(cursor.getColumnIndex("classification"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;

			}
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
		return issueGroupBean;
	}



	public static void delete(IssueGroupBean issueGroupBean) {
		DataBaseDB.getInst().getDB();
		String del = "delete from IssueGroupTable where id=?";
		db.beginTransaction();
		try {
			db.execSQL(del, new String[] { String.valueOf(issueGroupBean.id) });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}

	}

	/**
	 * 更新某个课题组信息
	 * 
	 * @param lm
	 */
	public static boolean update(IssueGroupBean issueGroupBean,int id) {
		DataBaseDB.getInst().getDB();
		String sql = "update IssueGroupTable set unit=?,principal=?,writer=?,area=?,valley=?,classification=? where id=?";
		db.beginTransaction();
		try {
			db.execSQL(
					sql,
					new String[] {issueGroupBean.unit,
							issueGroupBean.principal, issueGroupBean.writer,
							issueGroupBean.area, issueGroupBean.valley, issueGroupBean.classification,String.valueOf(id)});
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
		return true;
	}
}
